package org.ellwood.framework.common.result;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import org.ellwood.framework.common.core.enums.HttpCodeEnum;

import java.io.Serial;
import java.io.Serializable;

/**
 * @BelongsProject: ellwood-pro
 * @BelongsPackage: org.ellwood.framework.common.result
 * @BelongsClass: ResponseResult
 * @Description: // 统一响应基础类
 * @Author: ellwood
 * @CreateTime: 2024/1/30 - 16:18
 * @Version: V1.0.0
 */
@Schema(description = "统一响应基础类")
@Getter
@Setter
//@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseResult<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    @Schema(description = "响应码")
    private Integer code;
    @Schema(description = "响应信息")
    private String msg;
    @Schema(description = "响应数据")
    private T data;

    public ResponseResult() {
        this.code= HttpCodeEnum.SUCCESS.getCode();
        this.msg= HttpCodeEnum.SUCCESS.getMsg();
    }

    public ResponseResult(Integer code,T data){
        this.code=code;
        this.data=data;
    }

    public ResponseResult(Integer code,String msg){
        this.code=code;
        this.msg=msg;
    }

    public ResponseResult(Integer code,String msg,T data){
        this.code=code;
        this.msg=msg;
        this.data=data;
    }

    public static <T>  ResponseResult<T>  errorResult(int code,String msg){
        ResponseResult<T>  result = new ResponseResult<>();
        return result.error(code,msg);
    }

    public static <T>  ResponseResult<T>  okResult(){
        return new ResponseResult<T> ();
    }

    public static <T> ResponseResult<T>  okResult(int code,String msg){
        ResponseResult<T>  result = new ResponseResult<>();
        return result.ok(code,msg,null);
    }

    public static <T> ResponseResult<T>  okResult(T data){
        ResponseResult<T>  result = setHttpCodeEnum(HttpCodeEnum.SUCCESS, HttpCodeEnum.SUCCESS.getMsg());
        if (data!=null) {
            result.setData(data);
        }
        return result;
    }

    public static <T>  ResponseResult<T> errorResult(HttpCodeEnum codeEnum){
        return setHttpCodeEnum(codeEnum,codeEnum.getMsg());
    }

    private static <T>  ResponseResult<T>  errorResult(HttpCodeEnum codeEnum,String msg){
        return setHttpCodeEnum(codeEnum,msg);
    }

    public static <T> ResponseResult<T>  setHttpCodeEnum(HttpCodeEnum codeEnum){
        return okResult(codeEnum.getCode(),codeEnum.getMsg());
    }

    private static <T> ResponseResult<T> setHttpCodeEnum(HttpCodeEnum codeEnum,String msg){
        return okResult(codeEnum.getCode(),msg);
    }

    public ResponseResult<T> error(Integer code, String msg){
        this.code=code;
        this.msg=msg;
        return this;
    }

    public ResponseResult<T> ok(Integer code,String msg, T data){
        this.code=code;
        this.msg=msg;
        this.data=data;
        return this;
    }

    public ResponseResult<T>  ok(Integer code, T data){
        this.code=code;
        this.data=data;
        return this;
    }

    public ResponseResult<T>  ok(T data){
        this.data=data;
        return this;
    }

}
